package com.cxrj.zhsq.leaseManage.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.leaseManage.dao.PropertyGoodsMapper;
import com.cxrj.zhsq.leaseManage.dao.PropertyGoodsTypeMapper;
import com.cxrj.zhsq.leaseManage.entity.PropertyGoods;
import com.cxrj.zhsq.leaseManage.entity.PropertyGoodsType;
import com.cxrj.zhsq.leaseManage.service.GoodsService;

@Service
public class GoodsServiceImpl implements GoodsService {

	@Resource
	private PropertyGoodsMapper propertyGoodsMapper;

	@Resource
	private PropertyGoodsTypeMapper propertyGoodsTypeMapper;

	@Override
	public Map<String, Object> insertGoodsType(PropertyGoodsType propertyGoodsType) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if (propertyGoodsType.getId().equals("")) {
			propertyGoodsType.setId(UUID.randomUUID().toString());
			propertyGoodsType.setCreateTime(new Date());
			propertyGoodsType.setStatus("1");
			c = propertyGoodsTypeMapper.insertSelective(propertyGoodsType);
		} else {
			c = propertyGoodsTypeMapper.updateByPrimaryKeySelective(propertyGoodsType);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> seleteGoodsTypePage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		recordsTotal = propertyGoodsTypeMapper.selectByCount(sqlMap) + "";
		List<PropertyGoodsType> list = propertyGoodsTypeMapper.selectPropertyGoodsTypesPage(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> updateGoodsType(PropertyGoodsType propertyGoodsType) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = propertyGoodsTypeMapper.updateByPrimaryKeySelective(propertyGoodsType);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> seleteGoodsTypeById(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		PropertyGoodsType propertyGoodsType = propertyGoodsTypeMapper.selectByPrimaryKey(id);
		resultMap.put("goodsType", propertyGoodsType);
		return resultMap;
	}

	@Override
	public Map<String, Object> insertGoods(PropertyGoods propertyGoods) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if (propertyGoods.getId().equals("")) {
			propertyGoods.setId(UUID.randomUUID().toString());
			propertyGoods.setCreateTime(new Date());
			propertyGoods.setStatus("1");
			c = propertyGoodsMapper.insertSelective(propertyGoods);
		} else {
			c = propertyGoodsMapper.updateByPrimaryKeySelective(propertyGoods);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> seleteGoodsPage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		recordsTotal = propertyGoodsMapper.selectByCount(sqlMap) + "";
		List<PropertyGoods> list = propertyGoodsMapper.selectPropertyGoodssPage(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> updateGoods(PropertyGoods propertyGoods) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = propertyGoodsMapper.updateByPrimaryKeySelective(propertyGoods);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> seleteGoodsById(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		PropertyGoods propertyGoods = propertyGoodsMapper.selectByPrimaryKey(id);
		resultMap.put("goods", propertyGoods);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectGoodsTypeByAll(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		List<PropertyGoodsType> list = propertyGoodsTypeMapper.selectPropertyGoodsTypeByAll(sqlMap);
		resultMap.put("list", list);
		return resultMap;
	}

}
